package com.example.demo.implement;

import com.example.demo.config.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/")
public class TestController {

    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping(value ="/login", method = RequestMethod.GET)
    public Map<String, Object> first(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        request.getSession().setAttribute("url", request.getRequestURL());
        map.put("url", request.getRequestURL());
        return map;
    }

    @RequestMapping(value = "/sessions", method = RequestMethod.GET)
    public Object sessions (HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        map.put("sessionId", request.getSession().getId());
        map.put("message", request.getSession().getAttribute("url"));
        return map;
    }

    @RequestMapping(value = "/sessions", method = RequestMethod.POST)
    public String opRedis(){
        redisUtil.set("1",1);
        return "ok";
    }

}
